//
//  PCUserLoginViewController.m
//  PhotoCircle
//
//  Created by GL on 15/12/10.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCUserLoginViewController.h"
#import "GMString.h"
#import "PCUserModel.h"
#import "PCMeStore.h"
#import "CocoaSecurity.h"
#import "GMTipsHelper.h"
#import "PCUserRegisterViewController.h"

@interface PCUserLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextFile;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextFile;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation PCUserLoginViewController

-(void)configUI
{
    _registerButton.layer.cornerRadius = 5;
    _registerButton.layer.masksToBounds = YES;
    
    _loginButton.layer.cornerRadius = 3;
    _loginButton.layer.masksToBounds = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configUI];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    MCHiddenBottomView();
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)popViewController
{
    [self backEvent:nil];
}


#pragma mark -
#pragma mark -Button Event
- (IBAction)backEvent:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)resigterEvent:(UIButton *)sender {
    // 跳转到注册页面
    PCUserRegisterViewController * vc = [PCUserRegisterViewController controllerInStoryboard:@"Main" identifier:@"registerVC"];
    vc.type = @"注册";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)loginEvent:(UIButton *)sender {
    
    if ([NSString isEmptyString:self.phoneTextFile.text]) {
        return;
    }
    if ([NSString isEmptyString:self.passWordTextFile.text]) {
        return;
    }
    
    [GMTipsHelper showLoading];
    NSString *md5PassWord =[CocoaSecurity md5:self.passWordTextFile.text].hexLower;
    NSDictionary *pDic = @{@"phone":self.phoneTextFile.text,@"password":md5PassWord};

    [[PCMeStore sharedMeStore] requestUserLogin:pDic successHandler:^(NSInteger responCode){
        // 存储userInfo
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showLoadSeccess];
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:[PCMeStore sharedMeStore].userModel];
            [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.8];;
        }else{
            [GMTipsHelper showLoginFail];
        }
    } errorHandler:^{
        // 登录失败
        [GMTipsHelper showBadNetwork];
    }];
}

- (IBAction)forgetPassWord:(UIButton *)sender {
    
    PCUserRegisterViewController * vc = [PCUserRegisterViewController controllerInStoryboard:@"Main" identifier:@"registerVC"];
    vc.type = @"修改密码";
    [self.navigationController pushViewController:vc animated:YES];
}

@end
